package pl.edu.mimuw.mas.visualization.agent;

import java.util.HashSet;

import pl.edu.mimuw.mas.agent.AgentPossition;

/**
 * Zarządca wyświetlania agentów, uruchamiany w osobnym wątku.
 */
public class AgentPossitionManager extends Thread {
	private HashSet<AgentPossition> agents = new HashSet<AgentPossition>();
	private AgentPossition[] clone;
	private boolean modified = true;

	public void run() { }
	
	public AgentPossition[] getAllAgentPossitions() {
		synchronized (this) {
			if (modified) {
				clone = agents.toArray(new AgentPossition[agents.size()]);
				modified = false;
			}
		}

		return clone;
	}

	public void registerAgentPossition(AgentPossition ap) {
		if (ap != null) {
			synchronized (this) {
				modified = agents.add(ap);
			}
		}
	}

	public void deregisterAgentPossition(AgentPossition ap) {
		if (ap != null) {
			synchronized (this) {
				modified = agents.remove(ap);
			}
		}
	}
}
